/* QAD_ItemABCStatusReportUpdate.p - ABC ANALYSIS REPORT                      */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_ItemABCStatusReportUpdate.p 1046 2011-08-04 09:08:19Z jvz     $: */
/*                                                                            */
/* Report matches legacy ppptrp04.p for use by QAD Reporting Framework.       */
/*                                                                            */

{{&US_MF}mfdeclre.i}
{{&US_GP}gplabel.i}
{{&US_PX}pxmaint.i}
{{&US_PX}pxphdef.i ppitxr}
{{&US_PX}pxphdef.i cscsxr}
{{&US_MF}mf1.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* Temp-table definiation block */

define temp-table ttReportOptions no-undo
   field pt_prod_line like pt_mstr.pt_prod_line
   field si_site      like si_mstr.si_site
   field sls_iss      like lngd_det.lngd_mnemonic
   field cost_gross   like lngd_det.lngd_mnemonic
   field costset      like cs_mstr.cs_set
   field abc_yn       like mfc_ctrl.mfc_logical
   field avg_yn       like mfc_ctrl.mfc_logical
   field l_stat       as   character
   field a_yn         like mfc_ctrl.mfc_logical
   field a_int        like pt_mstr.pt_cyc_int
   field b_yn         like mfc_ctrl.mfc_logical
   field b_int        like pt_mstr.pt_cyc_int
   field c_yn         like mfc_ctrl.mfc_logical
   field c_int        like pt_mstr.pt_cyc_int
   field a_pct        as   decimal
   field b_pct        as   decimal
   field c_pct        as   decimal
.

define temp-table ttSite no-undo
   field unique_id   as   integer
   field si_site     like si_mstr.si_site
   field si_desc     like si_mstr.si_desc
.

define temp-table ttItemStatus no-undo
   field ref_id             as   integer
   field pti_abc            like pti_det.pti_abc
   field abc                like pti_det.pti_abc
   field pt_part            like pt_mstr.pt_part
   field glxcst             like sct_det.sct_cst_tot
   field gross_profit       like pt_mstr.pt_price
   field qty                like in_mstr.in_qty_oh
   field dollrs             as   decimal
   field dollar_percent     as   decimal
   field cum_dollar_percent as   decimal
   field cum_count_percent  as   decimal
   field description        as   character
.

define temp-table ttFooterMessage no-undo
   field msg_desc           as   character
.

define dataset dsReportResults for ttReportOptions,
                                   ttSite,
                                   ttItemStatus,
                                   ttFooterMessage.

define temp-table tt_abc no-undo
   field tt_part    like pt_mstr.pt_part
   field tt_site    like si_mstr.si_site
   field tt_qoh     as   decimal
   field tt_abc_amt like pt_mstr.pt_abc_amt
   field tt_abc_qty like pt_mstr.pt_abc_qty
   field tt_status  like pt_mstr.pt_status
   index part-site  is primary unique
                         tt_part
                         tt_site.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input  parameter runReport    as  logical no-undo.
define input  parameter reportHandle as  handle no-undo.
define input  parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

define variable default_cost_set like cs_mstr.cs_set no-undo.
define variable cost_user_input like cs_mstr.cs_set  no-undo.

{com/qad/shell/report/reporting.i}

define variable vhDS             as handle no-undo.

empty temp-table ttReportOptions no-error.
empty temp-table ttSite          no-error.
empty temp-table ttItemStatus    no-error.
empty Temp-table ttFooterMessage no-error.

{{&us_px}pxrun.i &PROC  = 'getStandardCostSet'
     &PROGRAM = 'cscsxr.p'
     &HANDLE=ph_cscsxr
     &PARAM = "  (input '',
                  input '',
                  output default_cost_set)"
     &NOAPPERROR = true
     &CATCHERROR = true
}


for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_ItemABCStatusReportUpdate.meta.xml").

   run SetReportMetaData(input reportHandle).

   cost_user_input = GetFilterValue(
           "ttReportOptions","costset",default_cost_set).
   if not can-find(first cs_mstr
      where cs_mstr.cs_domain = global_domain
      and cs_mstr.cs_set_isparent = no and cs_set = cost_user_input)
   then do:
     /*Invalid Child Cost Set*/
     {us/bbi/pxmsg.i &MSGNUM=25208 &ERRORLEVEL=3}
     return.
   end.

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.



PROCEDURE SetReportMetaData:

   define input parameter reportHandle as handle    no-undo.

   run SetFieldMetaParameter in reportHandle("ttReportOptions",
                  "costset", "DefaultValue", default_cost_set).

   run SetValueListMetaData(input reportHandle,
                            input "ppptrp04.p",
                            input "l_sls_iss",
                            input "sls_iss").

   run SetValueListMetaData(input reportHandle,
                            input "ppptrp04.p",
                            input "l_cost_gross",
                            input "cost_gross").


END PROCEDURE. /* PROCEDURE SetReportMetaData */

PROCEDURE SetValueListMetaData:

   define input parameter reportHandle as handle    no-undo.
   define input parameter vDataset     as character no-undo.
   define input parameter fName        as character no-undo.
   define input parameter fieldName    as character no-undo.
   define variable        valueList    as character no-undo.
   define variable        defaultValue as character no-undo.

   for each lngd_det no-lock
      where lngd_dataset = vDataset
      and   lngd_field   = fName
      and   lngd_lang    = global_user_lang:
      valueList = valueList + lngd_key2 + "," + lngd_key2 + ",".
      if lngd_key1 = "1" then do:
         defaultValue = lngd_key2.
      end.
   end.
   run SetFieldMetaParameter in reportHandle("ttReportOptions",
                                             fieldName,
                                             "valueList",
                                             valueList).

   run SetFieldMetaParameter in reportHandle("ttReportOptions",
                                             fieldName,
                                             "defaultValue",
                                             defaultValue).

END PROCEDURE. /* PROCEDURE SetValueListMetaData */


FUNCTION ValidateInput returns logical
   (input        sls_iss           as character,
    input        cost_gross        as character,
    input        l_stat            as character,
    input-output valid-mnemonic    as logical,
    input-output l_sls_iss         as character,
    input-output l_sls_iss_desc    as character,
    input-output l_cost_gross      as character,
    input-output l_cost_gross_desc as character):

   define variable i as integer no-undo.

   /* VALIDATE MNEMONICS */
   {gplngv.i
      &file     = ""ppptrp04.p""
      &field    = ""l_sls_iss""
      &mnemonic = sls_iss
      &isvalid  = valid-mnemonic}

   if valid-mnemonic = no
   then do:
      /* NOT A VALID CHOICE */
      {pxmsg.i &MSGNUM=13 &ERRORLEVEL=3}
      return no.
   end.

   {gplngv.i
      &file     = ""ppptrp04.p""
      &field    = ""l_cost_gross""
      &mnemonic = cost_gross
      &isvalid  = valid-mnemonic}

   if valid-mnemonic = no
   then do:
      /* NOT A VALID CHOICE */
      {pxmsg.i &MSGNUM=13 &ERRORLEVEL=3}
      return no.
   end.

   /* GET ATTRIBUTE TO STORE FOR THE MNEMONIC */
   {gplnga2n.i
      &file     = ""ppptrp04.p""
      &field    = ""l_sls_iss""
      &mnemonic = sls_iss
      &code     = l_sls_iss
      &label    = l_sls_iss_desc}

   {gplnga2n.i
      &file     = ""ppptrp04.p""
      &field    = ""l_cost_gross""
      &mnemonic = cost_gross
      &code     = l_cost_gross
      &label    = l_cost_gross_desc}

   /* VALIDATE LIST OF STATUSES TO EXCLUDE */
   if l_stat <> ""
   then do:
      do i = 1 to num-entries(l_stat):
         if not can-find (qad_wkfl no-lock
            where qad_domain = global_domain
              and qad_key1 = "PT_STATUS"
              and qad_key2 = entry(i, l_stat))
         then do:
            /* VALUE # (#) IN COMMA SEPARATED LIST IS INVALID */
            {pxmsg.i &MSGNUM=6442 &ERRORLEVEL=3
               &MSGARG1=i
               &MSGARG2=entry(i,l_stat)}
            return no.
         end. /*IF NOT CAN-FIND*/
      end. /*DO i = 1 TO NUM-ENTRIES*/
   end. /*IF l_stat <> "" */

   /** NOTE ANNUAL USAGE IS BASED ON: #, ANNUAL AMOUNT IS BASED ON: # */
   create ttFooterMessage.
   {pxmsg.i &MSGNUM=3781 &ERRORLEVEL=1 &MSGBUFFER=ttFooterMessage.msg_desc
            &MSGARG1=l_sls_iss_desc &MSGARG2=l_cost_gross_desc}

   return yes.

END FUNCTION.


FUNCTION ValidateSite returns logical
   (input ipCanRun as character):

   define variable authorized as integer initial 1 no-undo.
   define variable group_indx as integer           no-undo.

   if ipCanRun <> "" then do:
      authorized = 0.
      if can-do(ipCanRun + ",!*", global_userid) then do:
         authorized = 1.
      end.

      if authorized = 0
      then do group_indx = 1 to num-entries(global_user_groups)
      while authorized = 0:

         if can-do(ipCanRun + ",!*", entry(group_indx, global_user_groups))
         then do:
            authorized = 1.
         end.
      end.  /* DO WHILE */

   end.  /* IF  <> "" */

   return (authorized <> 0).

END FUNCTION.


/* Data retrieving logic block */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* Retrieve the data from database */
   define variable queryString         as   character     no-undo.
   define variable queryString2        as   character     no-undo.
   define variable hSiQuery            as   handle        no-undo.
   define variable hPtQuery            as   handle        no-undo.
   define query    SiQuery             for  si_mstr.
   define query    PtQuery             for  pt_mstr.
   define variable old_date_format     as   character     no-undo.

   define variable abc                 like pt_abc        no-undo.
   define variable abc_yn              like mfc_logical   no-undo.
   define variable avg_yn              like mfc_logical   no-undo.
   define variable total_sls_qty       as   decimal       no-undo.
   define variable total_iss_qty       as   decimal       no-undo.
   define variable tot_qty             as   decimal       no-undo.
   define variable tot_dollrs          as   decimal       no-undo.
   define variable tot_count           as   integer       no-undo.
   define variable qty                 like in_qty_oh     no-undo.
   define variable dollrs              as   decimal       no-undo.
   define variable a_yn                like mfc_logical   no-undo.
   define variable b_yn                like mfc_logical   no-undo.
   define variable c_yn                like mfc_logical   no-undo.
   define variable a_int               like pt_cyc_int    no-undo.
   define variable b_int               like pt_cyc_int    no-undo.
   define variable c_int               like pt_cyc_int    no-undo.
   define variable d_num               as   decimal       extent 3   no-undo.
   define variable i                   as   integer       no-undo.
   define variable gross_profit        like pt_price      no-undo.
   define variable dollar_percent      as   decimal       decimals 2 no-undo.
   define variable cum_dollar_percent  as   decimal       decimals 2 no-undo.
   define variable cum_count_percent   as   decimal       decimals 2 no-undo.
   define variable a_pct               as   decimal       no-undo.
   define variable b_pct               like a_pct         no-undo.
   define variable c_pct               like a_pct         no-undo.
   define variable sls_iss             like lngd_mnemonic no-undo.
   define variable l_sls_iss           as   character     no-undo.
   define variable l_sls_iss_desc      as   character     no-undo.
   define variable cost_gross          like lngd_mnemonic no-undo.
   define variable l_cost_gross        as   character     no-undo.
   define variable l_cost_gross_desc   as   character     no-undo.
   define variable valid-mnemonic      like mfc_logical   no-undo.
   define variable recidItemInventory  as   recid         no-undo.
   define variable l_stat              as   character     no-undo.
   define variable old_site            as   character     no-undo.
   define variable unique_id           as   integer       no-undo.
   define variable flag_need_update    like mfc_logical   no-undo.

   assign
      sls_iss              =  GetFilterValue("ttReportOptions",
                                                "sls_iss", "")
      cost_gross           =  GetFilterValue("ttReportOptions",
                                                "cost_gross", "")
      abc_yn               =  IsTrueValue("ttReportOptions", "abc_yn", yes)
      avg_yn               =  IsTrueValue("ttReportOptions", "avg_yn", yes)
      l_stat               =  GetFilterValue("ttReportOptions", "l_stat", "")
      a_yn                 =  IsTrueValue("ttReportOptions", "a_yn", no)
      a_int                =  integer(GetFilterValue("ttReportOptions",
                                                      "a_int", ?))
      b_yn                 =  IsTrueValue("ttReportOptions", "b_yn", no)
      b_int                =  integer(GetFilterValue("ttReportOptions",
                                                      "b_int", ?))
      c_yn                 =  IsTrueValue("ttReportOptions", "c_yn", no)
      c_int                =  integer(GetFilterValue("ttReportOptions",
                                                      "c_int", ?))
      a_pct                =  decimal(GetFilterValue("ttReportOptions",
                                                      "a_pct", ?))
      b_pct                =  decimal(GetFilterValue("ttReportOptions",
                                                      "b_pct", ?))
      c_pct                =  decimal(GetFilterValue("ttReportOptions",
                                                      "c_pct", ?))
      flag_need_update     =  abc_yn or avg_yn or a_yn or b_yn or c_yn
      hSiQuery             =  query SiQuery:handle
      hPtQuery             =  query PtQuery:handle
      old_date_format      =  session:date-format
      session:date-format  =  "ymd"
      old_site             =  hi_char.

   if not ValidateInput
      (input        sls_iss,
       input        cost_gross,
       input        l_stat,
       input-output valid-mnemonic,
       input-output l_sls_iss,
       input-output l_sls_iss_desc,
       input-output l_cost_gross,
       input-output l_cost_gross_desc)
   then do:
      return.
   end.

   queryString = "for each si_mstr where si_domain = " + quoter(global_domain).

   run AddAllConditions in reportHandle("si_mstr",
                                            input-output queryString).

   queryString = queryString + " no-lock break by si_site:".

   mainRepeat:
   do transaction on error undo:
   hSiQuery:query-prepare(queryString).
   hSiQuery:query-open().
   session:date-format = old_date_format.
   hSiQuery:get-next().

   repeat while not hSiQuery:query-off-end:

      if not ValidateSite(si_mstr.si_canrun)
      then do:
         hSiQuery:get-next().
         next.
      end.

      create ttSite.
      assign
         total_sls_qty        =  0
         total_iss_qty        =  0
         tot_dollrs           =  0
         tot_count            =  0
         tot_qty              =  0
         qty                  =  0
         dollrs               =  0
         gross_profit         =  0
         dollar_percent       =  0
         cum_dollar_percent   =  0
         cum_count_percent    =  0
         unique_id            =  unique_id + 1
         ttSite.unique_id     =  unique_id
         ttSite.si_site       =  si_mstr.si_site
         ttSite.si_desc       =  si_mstr.si_desc.

      do i = 1 to 3:
         d_num = 0.
      end.

      empty temp-table tt_abc no-error.

      queryString2 = "for each pt_mstr where pt_domain = "
                        + quoter(global_domain).

      run AddSomeConditions in reportHandle("pt_mstr", "a_int,b_int,c_int",
                                            input-output queryString2).

         hPtQuery:query-prepare(queryString2).
         hPtQuery:query-open().

         if flag_need_update then do:
            hPtQuery:get-next(exclusive-lock, no-wait).
         end.
         else do:
            hPtQuery:get-next().
         end.

         repeat while not hPtQuery:query-off-end:
            /* test if pt_mstr record is locked*/
            if flag_need_update then do:
               if locked pt_mstr then do:
                  {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                  undo mainRepeat, leave mainRepeat.
               end.
            end.

            if not can-find(in_mstr
                              where in_mstr.in_domain = global_domain
                                and in_mstr.in_part   = pt_mstr.pt_part
                                and in_mstr.in_site   = si_mstr.si_site)
            then do:
               if flag_need_update then do:
                  hPtQuery:get-next(exclusive-lock, no-wait).
               end.
               else do:
                  hPtQuery:get-next().
               end.
               next.
            end.

            total_sls_qty = 0.
            total_iss_qty = 0.

            if flag_need_update then do:
               /*need update database*/
               /*Note: Here no-wait option is needed.
                 So use find statement replace for statement. */
               find first in_mstr
                  where in_mstr.in_domain = global_domain
                    and in_mstr.in_part   = pt_mstr.pt_part
                    and in_mstr.in_site   = si_mstr.si_site
                    exclusive-lock no-wait no-error.
               if locked in_mstr then do:
                  {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                  undo mainRepeat , leave mainRepeat.
               end.
            end.
            else do:
               /*just browese*/
               for first in_mstr
                   where in_mstr.in_domain = global_domain
                   and in_mstr.in_part   = pt_mstr.pt_part
                   and in_mstr.in_site   = si_mstr.si_site no-lock:
               end.
            end.

            if available in_mstr then do:
               if flag_need_update then do:
                  find first pti_det
                     where pti_det.oid_pt_mstr  = pt_mstr.oid_pt_mstr
                       and pti_det.oid_si_mstr  = si_mstr.oid_si_mstr
                       exclusive-lock no-wait no-error.
                  if locked pti_det then do:
                     {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                     undo mainRepeat , leave mainRepeat.
                  end.
               end.
               else do:
                  for first pti_det no-lock
                     where pti_det.oid_pt_mstr  = pt_mstr.oid_pt_mstr
                        and pti_det.oid_si_mstr  = si_mstr.oid_si_mstr:
                  end.
               end.

               if available pti_det then do:
                  if (index(l_stat, pt_status)  <> 0
                     and index("abc", pti_det.pti_abc)  <> 0)
                  then do:
                     pti_det.pti_abc = "".
                  end.
                  else if (index("abc", pti_det.pti_abc) = 0
                         and pti_det.pti_abc <> " " )
                  then do:
                        if flag_need_update then do:
                           hPtQuery:get-next(exclusive-lock, no-wait).
                        end.
                        else do:
                           hPtQuery:get-next().
                        end.
                        next.
                  end.
               end. /* IF AVAILABLE pti_det */

               {pxrun.i &PROC  = 'readInventoryDetail' &PROGRAM = 'ppitxr.p'
                        &HANDLE=ph_ppitxr
                        &PARAM = "(input  pt_mstr.oid_pt_mstr,
                                   input  si_mstr.oid_si_mstr,
                                   buffer pti_det)"
                        &NOAPPERROR = true
                        &CATCHERROR = true
               }

               /* ALL OCCURRENCES OF IN_AVG_INT BELOW WERE ORIGINALLY
                * PT_AVG_INT */
               if avg_yn then do:

                  if available pti_det then do:
                     if in_avg_date = ? then do:
                        in_avg_date = today - 1.
                     end.

                     if today - in_avg_date > 0 and pti_avg_int <> 0 then do:
                        if in_avg_sls = 0 then do:
                           in_avg_sls = in_sls_chg / (today - in_avg_date).
                        end.
                        else do:
                           in_avg_sls = in_avg_sls *
                              max(0, 1 -  (today - in_avg_date) / pti_avg_int) +
                           (in_sls_chg /  (today - in_avg_date)) *
                              min(1, (today - in_avg_date) / pti_avg_int).
                        end.

                        if in_avg_iss = 0 then do:
                           in_avg_iss = in_iss_chg / (today - in_avg_date).
                        end.
                        else do:
                           in_avg_iss = in_avg_iss *
                              max(0, 1 -  (today - in_avg_date) / pti_avg_int) +
                           (in_iss_chg /  (today - in_avg_date)) *
                              min(1, (today - in_avg_date) / pti_avg_int).
                        end.
                     end. /* if today - in avg_date > 0 */
                  end. /* if available pti_det */
                  else if not available pti_det and available pt_mstr then do:
                     if in_avg_date = ? then do:
                        in_avg_date = today - 1.
                     end.

                     if today - in_avg_date > 0 and pt_avg_int <> 0 then do:
                        if in_avg_sls = 0 then do:
                           in_avg_sls = in_sls_chg / (today - in_avg_date).
                        end.
                        else do:
                           in_avg_sls = in_avg_sls *
                              max(0, 1 -  (today - in_avg_date) / pt_avg_int) +
                           (in_sls_chg /  (today - in_avg_date)) *
                              min(1, (today - in_avg_date) / pt_avg_int).
                        end.

                        if in_avg_iss = 0 then do:
                           in_avg_iss = in_iss_chg / (today - in_avg_date).
                        end.
                        else do:
                           in_avg_iss = in_avg_iss *
                              max(0, 1 -  (today - in_avg_date) / pt_avg_int) +
                           (in_iss_chg /  (today - in_avg_date)) *
                              min(1, (today - in_avg_date) / pt_avg_int).
                        end.
                     end. /* if today - in avg_date > 0 */
                  end. /* else if not available pti_det */

                  assign
                     in_avg_date = today
                     in_sls_chg = 0
                     in_iss_chg = 0.

               end. /* if avg_yn */

               total_sls_qty = total_sls_qty + in_avg_sls.
               total_iss_qty = total_iss_qty + in_avg_iss.

               /*{gpsct03.i &cost=sct_cst_tot}*/
               for first sct_det no-lock where
                  sct_det.sct_domain = global_domain
                  and sct_sim = cost_user_input
                  and sct_part = in_part
                  and sct_site = in_site:
                  assign
                     glxcst = sct_cst_tot
                     curcst = sct_cst_tot.
               end. /* for first sct_det */

            end. /* if available in_mstr*/

            assign
               qty    = if l_sls_iss = "1"
                        then
                           total_sls_qty * 365
                        else
                           total_iss_qty * 365

               dollrs = if l_cost_gross = "1"
                        then
                           glxcst
                        else
                           pt_price - glxcst.

            dollrs = dollrs * qty.

            assign
               tot_qty = tot_qty + qty
               tot_count = tot_count + 1
               tot_dollrs = tot_dollrs + dollrs.

            for first tt_abc
               where tt_abc.tt_part     = pt_mstr.pt_part
                 and tt_abc.tt_site    =  si_mstr.si_site:
            end.

            if not available tt_abc
            then do:
               create tt_abc.
               assign
                  tt_abc.tt_part    = pt_mstr.pt_part
                  tt_abc.tt_site    = si_mstr.si_site
                  tt_abc.tt_status  = pt_mstr.pt_status
                  tt_abc.tt_qoh     = (if available in_mstr
                                       then
                                          in_mstr.in_qty_oh
                                       else
                                          0)
                  tt_abc.tt_abc_amt = 0
                  tt_abc.tt_abc_qty = 0.
            end. /*tt_abc*/

            assign
               tt_abc.tt_abc_amt = dollrs
               tt_abc.tt_abc_qty = qty.

            if flag_need_update then do:
               hPtQuery:get-next(exclusive-lock, no-wait).
            end.
            else do:
               hPtQuery:get-next().
            end.
         end. /* for each pt_mstr */
  /*    end.  do transaction */

      for each tt_abc no-lock
         where can-find(in_mstr
                           where in_mstr.in_domain = global_domain
                             and in_mstr.in_part   = tt_part
                             and in_mstr.in_site   = si_mstr.si_site)
                             and (index(l_stat, tt_status) = 0
                                    or l_stat = "")
      break by tt_abc_amt descending
            by (if tt_qoh <> 0
                        then 1
                        else 2)
           by tt_part:
         for first pt_mstr no-lock
            where pt_mstr.pt_domain = global_domain
              and pt_mstr.pt_part   = tt_part:
         end.

         {pxrun.i &PROC  = 'readInventoryDetail' &PROGRAM = 'ppitxr.p'
                  &HANDLE=ph_ppitxr
                  &PARAM = "(input  pt_mstr.oid_pt_mstr,
                             input  si_mstr.oid_si_mstr,
                             buffer pti_det)"
                  &NOAPPERROR = true
                  &CATCHERROR = true
         }

         if available pti_det
            and (index("abc", pti_det.pti_abc) = 0
                 and pti_det.pti_abc <> " ")
         then do:
            hSiQuery:get-next().
            next.
         end.

         assign
           qty    = tt_abc.tt_abc_qty
           dollrs = tt_abc.tt_abc_amt.

         accumulate dollrs (total count).

         abc = "".
         if ((accum count dollrs) * 100) / tot_count <= a_pct
         then do:
            abc = "A".
         end.
         else if ((accum count dollrs) * 100) / tot_count <= a_pct + b_pct
         then do:
            abc = "B".
         end.
         else if ((accum count dollrs) * 100) / tot_count
            <= a_pct + b_pct + c_pct
         then do:
            abc = "C".
         end.

         if flag_need_update then do:
            find first in_mstr
               where in_mstr.in_domain = global_domain
                 and in_mstr.in_part   = pt_mstr.pt_part
                 and in_mstr.in_site   = si_mstr.si_site
                 exclusive-lock no-wait no-error.
            if locked in_mstr then do:
               {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
               undo mainRepeat , leave mainRepeat.
            end.
         end.
         else do:
            for first in_mstr no-lock
               where in_mstr.in_domain = global_domain
               and in_mstr.in_part   = pt_mstr.pt_part
               and in_mstr.in_site   = si_mstr.si_site:
            end.
         end.

         if available in_mstr then do:
            if flag_need_update then do:
               find first pti_det
                  where pti_det.oid_pt_mstr = pt_mstr.oid_pt_mstr
                  and   pti_det.oid_si_mstr = si_mstr.oid_si_mstr
                  exclusive-lock no-wait no-error.
               if locked pti_det then do:
                  {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                  undo mainRepeat , leave mainRepeat.
               end.
            end.
            else do:
               for first pti_det no-lock
                  where pti_det.oid_pt_mstr = pt_mstr.oid_pt_mstr
                  and   pti_det.oid_si_mstr = si_mstr.oid_si_mstr:
               end. /* FOR FIRST pti_det */
            end.
            /*{gpsct03.i &cost=sct_cst_tot}*/

            for first sct_det no-lock where
               sct_det.sct_domain = global_domain
               and sct_sim = cost_user_input
               and sct_part = in_part
               and sct_site = in_site:

               assign
                  glxcst = sct_cst_tot
                  curcst = sct_cst_tot.
            end. /* for first sct_det no-lock */

            gross_profit = pt_price - glxcst.

            dollar_percent = (dollrs / tot_dollrs) * 100.
            cum_dollar_percent = ((accum total dollrs) / tot_dollrs) * 100.
            cum_count_percent  = ((accum count dollrs) / tot_count) * 100.

            create ttItemStatus.
            assign
               ttItemStatus.ref_id             = unique_id
               ttItemStatus.pti_abc            = pti_det.pti_abc
                                                   when (available pti_det)
               ttItemStatus.abc                = abc
               ttItemStatus.pt_part            = pt_mstr.pt_part
               ttItemStatus.glxcst             = glxcst
               ttItemStatus.gross_profit       = gross_profit
               ttItemStatus.qty                = qty
               ttItemStatus.dollrs             = dollrs
               ttItemStatus.dollar_percent     = dollar_percent
               ttItemStatus.cum_dollar_percent = cum_dollar_percent
               ttItemStatus.cum_count_percent  = cum_count_percent
               ttItemStatus.description        = pt_desc1 + " " + pt_desc2.

            /* IF NO ITEM SITE INVENTORY RECORD EXIST THEN CREATE ONE */
            /* ONLY IF UPDATE ABC CLASS IS SET TO YES OR CYCLE COUNT IS */
            /* SET TO YES */
            if not available pti_det
               and available pt_mstr
               and (abc_yn or a_yn or b_yn or c_yn)
            then do:
               {pxrun.i &PROC  ='createItemInventoryDetail'
                        &PROGRAM='ppitxr.p'
                        &HANDLE=ph_ppitxr
                        &PARAM ="(input pt_mstr.oid_pt_mstr,
                                  input si_mstr.oid_si_mstr,
                                  output recidItemInventory)"
                        &NOAPPERROR=true
                        &CATCHERROR=true}
               if flag_need_update then do:
                  find first pti_det
                     where recid(pti_det) = recidItemInventory
                     exclusive-lock no-wait no-error.
                  if locked pti_det then do:
                     {pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                     undo mainRepeat , leave mainRepeat.
                  end.
               end.
               else do:
                  for first pti_det no-lock
                     where recid(pti_det) = recidItemInventory:
                  end.
               end.
            end. /* if available pti_det */

            if available pti_det
               and abc_yn
               and (pti_det.pti_abc = "A"
                     or pti_det.pti_abc = "B"
                     or pti_det.pti_abc = "C"
                     or pti_det.pti_abc = " "
                     or pti_det.pti_abc = "")
            then do:
               pti_det.pti_abc = abc.
            end.

            if available pti_det and (a_yn or b_yn or c_yn)
            then do:
               if pti_det.pti_abc = "A" and a_yn and a_int <> ? then do:
                  pti_cyc_int = a_int.
               end.
               else if pti_det.pti_abc = "B" and b_yn and b_int <> ? then do:
                  pti_cyc_int = b_int.
               end.
               else if pti_det.pti_abc = "C" and c_yn and c_int <> ? then do:
                  pti_cyc_int = c_int.
               end.
            end. /* if a_yn */

         end. /* if avail in_mstr */

      end. /* for each tt_abc no-lock */

      hPtQuery:query-close().
      hSiQuery:get-next().
   end. /* Repeat query */
   end. /* Do transaction */
   hSiQuery:query-close().
   delete object hPtQuery.
   delete object hSiQuery.

   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.

